// ignore_for_file: unused_local_variable, avoid_unnecessary_containers

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/app_permission/permission_widget.dart';
import 'package:yt_devops/components/tips.dart';
import 'package:yt_devops/pages/mine/message_center/advertise_message/advertise_message_controller.dart';
import 'package:yt_devops/pages/mine/message_center/message_center_controller.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:badges/badges.dart' as badges;
import 'package:yt_devops/utils/constants/permission_page_constant.dart';
import 'package:yt_devops/utils/enums.dart';

class AdvertiseMessagePage extends GetView<AdvertiseMessageController> {
  AdvertiseMessagePage({Key? key}) : super(key: key);
  @override
  final controller = Get.put(AdvertiseMessageController());
  final messageCenterController = Get.put(MessageCenterController());
  final state = Get.find<AdvertiseMessageController>().state;

  Widget _generateItem(context, index) {
    return InkWell(
      onTap: () => controller.goMessageDetail(index),
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.symmetric(
              vertical: 14.h,
              horizontal: 16.w,
            ),
            height: 72.h,
            width: double.infinity,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Stack(
                  children: [
                    Container(
                      width: 40.w,
                      height: 40.w,
                      decoration: const BoxDecoration(
                        image: DecorationImage(
                          fit: BoxFit.cover,
                          image: AssetImage(
                            ImageAssets.advertiseMessageLogo,
                          ),
                        ),
                      ),
                    ),
                    Positioned(
                      right: 0,
                      child: badges.Badge(
                        position: badges.BadgePosition.topStart(
                          top: 2.h,
                        ),
                        showBadge: controller.messagesList[index].status == AdvertiseStatusType.normal.value,
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  width: 12.w,
                  height: 72.h,
                ),
                Expanded(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Text(
                            controller.messagesList[index].noticeTitle!,
                            style: TextStyle(
                              color: Colour.mainTextColor,
                              fontSize: 16.sp,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                          Text(
                            controller.messagesList[index].createTime!.replaceAll('-', '/'),
                            style: TextStyle(
                              color: Colour.lightTextColor,
                              fontSize: 11.sp,
                            ),
                          ),
                        ],
                      ),
                      Container(
                        child: Text(
                          controller.messagesList[index].noticeTitle!,
                          style: TextStyle(
                            color: Colour.mainTextColor,
                            fontSize: 12.sp,
                            fontWeight: FontWeight.normal,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          Container(height: 0.5,color: Colour.dividerColor,margin: EdgeInsets.symmetric(horizontal: 16),)
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AdvertiseMessageController>(
      builder: (_) {
        return WillPopScope(
          onWillPop: () async {
            messageCenterController.onReady();
            return true;
          },
          child:  Scaffold(
            backgroundColor: Colour.white,
              appBar: AppBarWidget.withRightActions(
                ResKey.advertiseMessage.tr,
                elevation:0.1,
                actions: [
                  PermissionWidget(
                    permissionStringList: const [PermissionConstant.mineMessageCenterAdvertiseMessageAllRead],
                    child: GestureDetector(
                      onTap: () => controller.showCupertinoAlertDialog(context),
                      child: Padding(
                        padding: EdgeInsets.symmetric(
                          horizontal: 16.w,
                          vertical: 16.h,
                        ),
                        child: Text(
                          ResKey.allRead.tr,
                          style: TextStyle(
                            color: Colour.mainTextColor,
                            fontSize: 14.sp,
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              body:  SmartRefresher(
                    controller: controller.refreshController,
                    enablePullDown: true,
                    enablePullUp: true,
                    header: ListTips.classicHeader,
                    footer: ListTips.generateClassicFooter,
                    onRefresh: controller.onRefresh,
                    onLoading: controller.onLoad,
                    child:  ListView.builder(
                      // controller: controller.scrollController,
                      padding: EdgeInsets.zero,
                      itemBuilder: (BuildContext context, int index) {
                        return _generateItem(context, index);
                      },
                      itemCount: controller.messagesList.length,
                    )
                  ),
                ),
        );
      },
    );
  }
}
